#include "head.h"
#include <sys/types.h>
#include <sys/socket.h>
//#include <sys/epoll.h>
#include <sqlite3.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#include "server.h"

#define ERRLOG(errmsg)                                          \
    do                                                          \
    {                                                           \
        perror(errmsg);                                         \
        printf("%s--%s--(%d)\n", __FILE__, __func__, __LINE__); \
        exit(-1);                                               \
    } while (0)
enum
{
    USER_LOGIN = 1,
    ADMIN_LOGIN,
    QUIT,
    USER_SELECT = 1,
    USER_QUIT,
    ADMIN_REG = 1,
    ADMIN_ADD,
    ADMIN_DEL,
    ADMIN_MODFY,
    ADMIN_SELECT,
    ADMIN_QUIT,
    ADMIN_S1 = 1,
    ADMIN_S2,
    ADMIN_Q
};

int temp_id;
void print_load_menu()
{
    printf("**************欢 迎 使 用 员 工 管 理 系 统*******************\n");
    printf("************************************************************\n");
    printf("**************1.用户登录***2.管理员登录***3.退出**************\n");
    printf("************************************************************\n");
}
void print_user_menu()
{
    printf("****************欢 迎 使 用 员 工 管 理 系 统*****************\n");
    printf("************************************************************\n");
    printf("****************** 1.查询 ****** 2.退出 *********************\n");
    printf("************************************************************\n");
}
void print_admin_menu()
{
    printf("***************欢 迎 使 用 员 工 管 理 系 统******************\n");
    printf("************************************************************\n");
    printf("*** 1.注册 ** 2.添加 ** 3.删除 ** 4.修改** 5.查询 ** 6.退出***\n");
    printf("************************************************************\n");
}
void print_admin_select_menu()
{
    printf("***************欢 迎 使 用 员 工 管 理 系 统******************\n");
    printf("************************************************************\n");
    printf("************** 1.查询 ***2.查询所有***3.退出 ****************\n");
    printf("************************************************************\n");
}
void user_login_func(int skfd, msg_t *msg)
{
    int id;
    char passwd[20];
    char buf[128] = {0};
    while (1)
    {
        printf("input your id >> ");
        scanf("%d", &id);
        getchar();
        temp_id = id;
        printf("input your passwd >> ");
        scanf("%s", passwd);
        getchar();

        msg->code = 'L';
        msg->id = id;
        strcpy(msg->text, passwd);
        if (-1 == write(skfd, msg, sizeof(msg_t)))
        {
            ERRLOG("write error\n");
        }
        printf("***************发送数据\n");
        if (-1 == read(skfd, msg, sizeof(msg_t)))
        {
            ERRLOG("read error\n");
        }
        printf("msg->text=%s\n", msg->text);
        if (!strcmp(msg->text, "OK"))
        {
            printf("%d成功登录\n", msg->id);
            break;
        }
        else if (!strcmp(msg->text, "repeat login"))
        {
            printf("you have logged in and cannot log in again\n");
            continue;
        }
        else
        {
            printf("id or passwd error,try again\n");
            continue;
        }
    }
}
void user_select_func(int skfd, msg_t *msg)
{
    msg->code = 'Q';
    msg->role = 0;
    msg->id = temp_id;
    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("read error\n");
    }
    if (strcmp(msg->text, "OK") == 0)
    {
        printf("id=%d,name=%s,sex=%c,salary=%d\n", msg->id, msg->name, msg->sex, msg->salary);
    }
    else
    {
        printf("查询失败，请重试\n");
    }
}
void admin_select_func(int skfd, msg_t *msg)
{
    msg->code = 'Q';
    msg->role = 1;
    printf("请输入要查询的员工ID>> ");
    scanf("%d", &msg->id);
    getchar();

    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("read error\n");
    }
    if (strcmp(msg->text, "OK") == 0)
    {
        printf("id=%d,name=%s,sex=%c,salary=%d\n", msg->id, msg->name, msg->sex, msg->salary);
    }
    else
    {
        printf("查询失败，请重试\n");
    }
}
void admin_select_all_func(int skfd, msg_t *msg)
{
    msg->code = 'S';
    msg->role = 1;
    strcpy(msg->text, "all");

    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    while (1)
    {

        if (-1 == read(skfd, msg, sizeof(msg_t)))
        {
            ERRLOG("read error\n");
        }
        if (strcmp(msg->text, "OK") == 0)
        {
            if (-1 == read(skfd, msg, sizeof(msg_t)))
            {
                ERRLOG("read error\n");
            }
            while (strcmp(msg->text, "**over**") != 0)
            {

                printf("%-5s\n", msg->text);
                if (-1 == read(skfd, msg, sizeof(msg_t)))
                {
                    ERRLOG("read error\n");
                }
            }
        }
        else
        {
            printf("查询失败，请重试\n");
        }
    }
}

void quit_func(int skfd, msg_t *msg)
{
    msg->code = 'Q';
    msg->id = temp_id;
    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("recv error\n");
    }
    if (!strcmp(msg->text, "OK"))
    {
        printf("退出员工管理系统\n");
        exit(1);
    }
    else
    {
        printf("退出失败，请重试\n");
    }
}
void admin_add_func(int skfd, msg_t *msg)
{
    msg->code = 'A';
    // printf("请输入要添加的员工 ID (INT)>> ");
    // scanf("%d", &msg->id);
    // getchar();
    printf("请输入要添加的员工 姓名 (TEXT)>> ");
    scanf("%s", msg->name);
    getchar();
    printf("请输入要添加的员工 性别 (TEXT)>> ");
    scanf("%c", &msg->sex);
    getchar();
    printf("请输入要添加的员工 工资 (INT)>> ");
    scanf("%d", &msg->salary);
    getchar();
    printf("请输入要添加的员工 权限 (普通用户 0 管理员 1)>> ");
    scanf("%d", &msg->role);
    getchar();
    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("read error\n");
    }
    if (!strcmp(msg->text, "OK"))
    {
        printf("添加成功\n");
    }
    else
    {
        printf("添加失败，请重试\n");
    }
}
void admin_delete_func(int skfd, msg_t *msg)
{
    msg->code = 'D';
    printf("请输入要删除的员工 ID (INT)>> ");
    scanf("%d", &msg->id);
    getchar();
    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("read error\n");
    }
    if (!strcmp(msg->text, "OK"))
    {
        printf("删除成功\n");
    }
    else
    {
        printf("删除失败，请重试\n");
    }
}
void admin_modify_func(int skfd, msg_t *msg)
{
    msg->code = 'M';
    printf("请输入要修改的员工 ID (INT)>> ");
    scanf("%d", &msg->id);
    getchar();
    printf("请输入新的员工 姓名 (TEXT)>> ");
    scanf("%s", msg->name);
    getchar();
    printf("请输入新的员工 性别 (TEXT)>> ");
    scanf("%c", &msg->sex);
    getchar();
    printf("请输入新的员工 工资 (INT)>> ");
    scanf("%d", &msg->salary);
    getchar();
    printf("请输入新的员工 权限 (普通用户 0 管理员 1)>> ");
    scanf("%d", &msg->role);
    getchar();
    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("read error\n");
    }
    if (!strcmp(msg->text, "OK"))
    {
        printf("修改成功\n");
    }
    else
    {
        printf("修改失败，请重试\n");
    }
}
void admin_register_func(int skfd, msg_t *msg)
{
    msg->code = 'R';
    printf("请输入注册的员工 ID (INT)>> ");
    scanf("%d", &msg->id);
    getchar();
    printf("请输入初始 密码 (TEXT)>> ");
    scanf("%s", msg->text);
    getchar();
    printf("请输入新员工 权限 (普通用户 0 管理员 1)>> ");
    scanf("%d", &msg->role);
    getchar();
    printf("code=%d,id=%d,passwd=%s,role=%d\n", msg->code, msg->id, msg->text, msg->role);
    if (-1 == write(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("write error\n");
    }
    if (-1 == read(skfd, msg, sizeof(msg_t)))
    {
        ERRLOG("read error\n");
    }
    printf("id=%d,msg->text=%s\n", msg->id, msg->text);
    if (!strcmp(msg->text, "OK"))
    {
        printf("注册成功\n");
    }
    else
    {
        printf("注册失败，请重试\n");
    }
}
int main(int argc, const char *argv[])
{
    if (argc != 3)
    {
        printf("usage %s <ip> <port> \n", argv[0]);
        exit(-1);
    }
    //创建套接字
    int skfd;
    if (-1 == (skfd = socket(AF_INET, SOCK_STREAM, 0)))
    {
        ERRLOG("socket error\n");
    }
    //填充服务器网络信息结构体
    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    socklen_t addrlen = sizeof(serveraddr);

    if (-1 == connect(skfd, (struct sockaddr *)&serveraddr, addrlen))
    {
        ERRLOG("connect error\n");
    }
    printf("与服务器建议连接\n");

    int choose;
    msg_t msg;
    while (1)
    {
        print_load_menu();
        printf("input your choose>> ");
        scanf("%d", &choose);
        getchar();
        switch (choose)
        {
        case USER_LOGIN:
            //普通用户登录的函数
            user_login_func(skfd, &msg);
            goto NEXT1;
            break;
        case ADMIN_LOGIN:
            //管理员登录的函数
            user_login_func(skfd, &msg);
            goto NEXT2;
            break;
        case QUIT:
            //管理员登录的函数
            user_login_func(skfd, &msg);
            goto NEXT2;
            break;
        }
    }
NEXT1:
    while (1)
    {
        print_user_menu();
        printf("input your choose>> ");
        scanf("%d", &choose);
        getchar();
        switch (choose)
        {
        case USER_SELECT:
            //普通用户查询
            user_select_func(skfd, &msg);
            break;
        case USER_QUIT:
            break;
        }
    }
NEXT2:
    while (1)
    {
        print_admin_menu();
        printf("input your choose>> ");
        scanf("%d", &choose);
        getchar();
        switch (choose)
        {
        case ADMIN_ADD:
            admin_add_func(skfd, &msg);
            continue;
        case ADMIN_DEL:
            admin_delete_func(skfd, &msg);
            continue;
        case ADMIN_MODFY:
            admin_modify_func(skfd, &msg);
            continue;
        case ADMIN_SELECT:
            goto NEXT3;
        case ADMIN_REG:
            admin_register_func(skfd, &msg);
            continue;
        case ADMIN_QUIT:
            quit_func(skfd, &msg);
            break;
        }
    }
NEXT3:
    while (1)
    {
        print_admin_select_menu();
        printf("input your choose>> ");
        scanf("%d", &choose);
        getchar();
        switch (choose)
        {
        case ADMIN_S1:
            admin_select_func(skfd, &msg);
            continue;
        case ADMIN_S2:
            admin_select_all_func(skfd, &msg);
            continue;
        case ADMIN_Q:
            goto NEXT2;
            break;
        }
    }
    return 0;
}